﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication1
{
    public partial class ViewSchool : System.Web.UI.Page
    {
        public string strBriefInfo
        {
            set;
            get;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["SchoolId"] == null)
            {
                Response.Redirect("~/Search.aspx");
            }
            if (Session["UserInfo"] == null||((Entity.UserInfo)Session["UserInfo"]).UserType == "School")
            {
                CommentWarning.Visible = true;
                ibOK.Enabled = false;
                tbxComment.Enabled = false;
            }
            else
            {
                CommentWarning.Visible = false;
                ibOK.Enabled = true;
                tbxComment.Enabled = true;
            }

            if (!IsPostBack)
            {
                tbxSearch.Attributes.Add("onclick", "this.value=''");
                int iSchoolId = Convert.ToInt32(Request.QueryString["SchoolId"]);
                DataTable dt = new DataTable();
                SQLHelper.getSchoolInfoById(iSchoolId, out dt);
                RepeaterSchoolInfo.DataSource = dt;
                RepeaterSchoolInfo.DataBind();

                string path = "?SchoolId=" + Request.QueryString["SchoolId"];
                BindData(Convert.ToInt32(Request.QueryString["SchoolId"]), path);
                //DataTable dtCommnet = new DataTable();
                //SQLHelper.getCommentBySchoolId(iSchoolId, out dtCommnet);
                //RepeaterComment.DataSource = dtCommnet;
                //RepeaterComment.DataBind();
                
                strBriefInfo = dt.Rows[0]["BriefInfo"].ToString();
                DataTable dt_TopCourse = new DataTable();
                SQLHelper.getTopCourseBySchoolId(iSchoolId, out dt_TopCourse);
                RepeaterTopCourse.DataSource = dt_TopCourse ;
                RepeaterTopCourse.DataBind();
                Page.DataBind();

                SQLHelper.AddSchoolPopularityOnePointBySchoolId(iSchoolId);
            }

        }

        protected void RepeaterSchoolInfo_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //判断里层repeater处于外层repeater的哪个位置（ AlternatingItemTemplate，FooterTemplate，

            //HeaderTemplate，，ItemTemplate，SeparatorTemplate）
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rep = e.Item.FindControl("RepeaterTag") as Repeater;//找到里层的repeater对象
                DataRowView rowv = (DataRowView)e.Item.DataItem;//找到分类Repeater关联的数据项 
                string text = rowv["Id"].ToString(); //获取填充子类的id 
                DataTable dt = new DataTable();
                SQLHelper.getTagsBySchoolId(Convert.ToInt32(text), out dt);
                rep.DataSource = dt;
                rep.DataBind();
            }

        }


        private void BindData(int iSchoolId, string path)
        {
            DataTable dt = new DataTable();
            SQLHelper.getCommentBySchoolId(iSchoolId, out dt);
            

            PagedDataSource pds = new PagedDataSource();

            DataView dataView = dt.DefaultView;

            pds.DataSource = dataView;
            //pds.AllowCustomPaging = true;
            pds.AllowPaging = true;
            //pds.AllowServerPaging = true;
            pds.PageSize = 5;


            int CurPage;
            if (Request.QueryString["Page"] != null)
                CurPage = Convert.ToInt32(Request.QueryString["Page"]);
            else
                CurPage = 1;
            int TotalCount = 0;//总记录数
            int TotalPage = 1; //总页数


            TotalCount = dt.DefaultView.Count;

            if (TotalCount == 0)
                TotalPage = 1;
            else
            {
                if (TotalCount % pds.PageSize == 0)
                    TotalPage = TotalCount / pds.PageSize;
                else
                    TotalPage = TotalCount / pds.PageSize + 1;
            }

            pds.CurrentPageIndex = CurPage - 1;
            lblCurrentPage.Text = "共" + TotalCount.ToString() + "条记录 当前页：" + CurPage.ToString() + "/" + TotalPage;

            lnkFrist.NavigateUrl = Request.CurrentExecutionFilePath + path + "&Page=1";
            if (!pds.IsFirstPage)
                lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + path + "&Page=" + Convert.ToString(CurPage - 1);

            if (!pds.IsLastPage)
                lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + path + "&Page=" + Convert.ToString(CurPage + 1);
            lnkEnd.NavigateUrl = Request.CurrentExecutionFilePath + path + "&Page=" + TotalPage;


            RepeaterComment.DataSource = pds;
            RepeaterComment.DataBind();

        }


        protected void ibtMoreCourse_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("MoreCourse.aspx?SchoolId=" + Request.QueryString["SchoolId"]);
        }

       

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Search.aspx?text=" + tbxSearch.Text);
        }

        protected void ibOK_Click(object sender, ImageClickEventArgs e)
        {
            if (Session["UserInfo"] == null)
            {
                Response.Write("<script>alert('请先登录!');location.href='Login.aspx';</script>");
                return;
            }
            if (tbxComment.Text == "")
            {
                Response.Write("<script language = javascript>alert('评论不能为空');</script>");
            }
            
            SQLHelper.AddComment(tbxComment.Text, float.Parse(HiddenField1.Value), float.Parse(HiddenField2.Value), float.Parse(HiddenField3.Value), float.Parse(HiddenField4.Value), ((Entity.UserInfo)Session["UserInfo"]).UserId, Convert.ToInt32(Request.QueryString["SchoolId"]), DateTime.Now.ToString());
            Response.Write("<script>alert('评论添加成功!');location.href='"+Request.Url.ToString()+"';</script>");
            return;
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Login.aspx");
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            Response.Redirect("regUser.aspx");
        }



        protected void ibCorrect_Click(object sender, ImageClickEventArgs e)
        {
            int iSchoolId = Convert.ToInt32(Request.QueryString["SchoolId"]);
            SQLHelper.addCorrectInfo(iSchoolId, CheckBox_Address.Checked, CheckBox_Tel.Checked, CheckBox_Map.Checked, CheckBox_Tag.Checked);
            Response.Write("<script>alert('我们已录入您的纠错信息，会尽快查实，谢谢您的支持!');location.href='" + Request.Url.ToString() + "';</script>");
            return;
            
        }
    }
}